import io.github.lnyocly.ai4j.config.MoonshotConfig;
import io.github.lnyocly.ai4j.config.OpenAiConfig;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletion;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletionResponse;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatMessage;
import io.github.lnyocly.ai4j.service.Configuration;
import io.github.lnyocly.ai4j.service.IChatService;
import io.github.lnyocly.ai4j.service.PlatformType;
import io.github.lnyocly.ai4j.service.factor.AiService;
import okhttp3.OkHttpClient;

public class OpenaiChat {

    private final OkHttpClient okHttpClient;
    private final String system;

    public OpenaiChat(OkHttpClient okHttpClient, String system) {
        this.okHttpClient = okHttpClient;
        this.system = system;
    }

    public static void main(String[] args) throws Exception {
        OpenaiChat kimiChat = new OpenaiChat(Ai4jTest.okHttpClient, "百科");
        ChatCompletionResponse response = kimiChat.chat("你好是什么意思");
        System.out.println(response.getChoices().get(0).getMessage().getContent().getText());
    }

    public ChatCompletionResponse chat(String content) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setOkHttpClient(okHttpClient);

        OpenAiConfig openAiConfig = new OpenAiConfig();
        configuration.setOpenAiConfig(openAiConfig);
        openAiConfig.setApiHost("https://NeiBuZhiCheng55.openai.azure.com/openai/deployments/gpt-4o-2024-05-13/");
        openAiConfig.setChatCompletionUrl("chat/completions?api-version=2024-08-01-preview");
        openAiConfig.setApiKey("5485be6fb11e47858413428d2873f967");


        AiService aiService = new AiService(configuration);
        IChatService ds = aiService.getChatService(PlatformType.OPENAI);

        // 构建请求参数
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-2024-05-13")
                .message(ChatMessage.withSystem(system))
                .message(ChatMessage.withUser(content))
                .build();

        // 发送对话请求
        return ds.chatCompletion(chatCompletion);
    }

}
